// 02order/orderDetail/orderDetail.js
var co = require("../../resource/js/common.js")
var MD5 = require("../../resource/js/md5.js")
var ws = require("../../resource/js/websocket.js")
var app = getApp()
var _this, _data, _appid, _formId
Page({
   data: {
      orderID: "",
      orderList: [],
      popMenuIsShow: false, //选择支付方式弹窗
      userBalance: '', //用户余额
      MoneyIsShow: false, //输入金额的显示
      showInputPwd: false, //显示密码输入
      Length: 6, //输入框个数
      isFocus: true, //聚焦
      Value: "", //输入的内容
      ispassword: true, //是否密文显示 true为密文， false为明文。
      InputMoneyValue: null, //输入框的钱
      payMoney: null, //支付的钱
      Extract: null, //提成费
      index: null, //选择方式下标
      schedule: '', //进度
   },
   onLoad: function(opt) {
      var orders = JSON.parse(opt.Order_ID)
      _this = this
      _data = _this.data
      _appid = app.Data.app.appid
      _data.orderID = orders.Order_ID
      _this.getOrderDetail()
   },
   //获取客户订单详情信息
   getOrderDetail() {
      co.getJyhsApi("order/Order/getOrderDetails", {
         Order_ID: _data.orderID
      }, function(res) {
         if (res.code == 0) {
            _this.setData({
               orderList: res.data
            })
            if (res.data.Order_Status == 0) { //未接单
               _this.setData({
                  schedule: 0
               })
            } else if (res.data.Order_Status == 1) { //已接单
               _this.setData({
                  schedule: 1
               })
            } else if (res.data.Order_Status == 2) { //已完成
               _this.setData({
                  schedule: 4
               })

            } else if (res.data.Order_Status == 3) { //已评价
               _this.setData({
                  schedule: 5
               })

            } else if (res.data.Order_Status == 4) { //已支付
               _this.setData({
                  schedule: 3
               })
            } else if (res.data.Order_Status == 5) { //待上门
               _this.setData({
                  schedule: 2
               })
            }
         }
      })
   },
   //导航路线
   showMapLocation: function() {
      wx.openLocation({
         latitude: parseFloat(_data.orderList.Biz_Info.Biz_PrimaryLat) ? parseFloat(_data.orderList.Biz_Info.Biz_PrimaryLat) : parseFloat(_data.orderList.User_Info.User_PrimaryLat),
         longitude: parseFloat(_data.orderList.Biz_Info.Biz_PrimaryLng) ? parseFloat(_data.orderList.Biz_Info.Biz_PrimaryLng) : parseFloat(_data.orderList.User_Info.User_PrimaryLng),
         scale: '',
         name: '',
         address: '',
         success: function(res) {
            console.log('【跳转地图再跳导航】')
         },
         fail: function(res) {},
      })
   },
   //回收端人员支付
   pay() {
      _this.setData({
         popMenuIsShow: true,
         userBalance: app.Data.userInfo.User_Balance,
      })
   },
   //关闭支付方式弹窗
   closePopShow: function(e) {
      _this.setData({
         popMenuIsShow: false,
      });
   },
   //弹出输入金额事件
   inputMoney(e) {
      var payPassword = app.Data.userInfo.User_PayPassword
      if (co.empty(payPassword)) {
         wx.navigateTo({
            url: '/04jyhs/setting/accountNumber/accountNumber',
            success: function() {
               co.toastAllText("请先设置支付密码")
            }
         })
      } else {
         _data.index = null
         var index = e.currentTarget.dataset.id
         if (parseFloat(_data.userBalance) > 0) {
            _this.setData({
               popMenuIsShow: false,
               MoneyIsShow: true,
               index: index
            })
         } else {
            co.toastAllText("余额不足请充值！")
            wx.navigateTo({
               url: '/04jyhs/accountAdmin/rechange/rechange',
            })
         }
      }
   },
   //关闭金额模态框
   closeMoney() {
      _this.setData({
         MoneyIsShow: false,
      });
   },
   //弹出密码框
   inputPwd() {
      _data.Value = null
      var total1 = parseFloat(_data.InputMoneyValue) + parseFloat(_data.InputMoneyValue) * parseFloat(_data.orderList.Biz_Fee_Value)
      var total = total1.toFixed(2)
      if (_data.index == "1") { //余额支付
         if (parseFloat(_data.userBalance) >= parseFloat(_data.InputMoneyValue)) {
            _this.setData({
               showInputPwd: true,
               MoneyIsShow: false,
               payMoney: total,
               Extract: (parseFloat(_data.InputMoneyValue) * parseFloat(_data.orderList.Biz_Fee_Value)).toPrecision(2)
            })
         } else {
            co.toastAllText("余额不足请充值！")
         }
      } else { //微信支付
         _this.setData({
            MoneyIsShow: false,
            payMoney: total,
            Extract: (parseFloat(_data.InputMoneyValue) * parseFloat(_data.orderList.Biz_Fee_Value)).toPrecision(2)
         })
         _this.wxPay()
      }
   },
   //关闭密码框
   closePwdShow() {
      _this.setData({
         showInputPwd: false,
      });
   },
   //监听输入金额
   MoneyInput(e) {
      _this.setData({
         InputMoneyValue: "",
      })
      var val = e.detail.value
      _this.setData({
         InputMoneyValue: co.filterStr(val, '.'),
      })
   },
   Focus(e) {
      var that = this;
      var inputValue = e.detail.value;
      that.setData({
         Value: inputValue,
      })
   },
   Tap() {
      var that = this;
      that.setData({
         isFocus: true,
      })
   },
   //余额支付
   balancePay(e) {
      _formId = e.detail.formId
      _this.setData({
         showInputPwd: false,
      });
      var pwd = MD5.md5(e.detail.value.password) //支付密码
      wx.showLoading({
         title: '正在支付...',
      })
      var param = {
         Pay_From_ID: 'User_' + app.Data.userInfo.User_ID,
         Pay_Password: pwd,
         Pay_To_ID: _data.orderList.Biz_ID == 0 ? 'User_' + _data.orderList.User_ID : 'Biz_' + _data.orderList.Biz_ID,
         Abstract: _data.orderList.Reclaim_User_Info.User_NickName + "订单,已支付" + _data.payMoney,
         Money: _data.payMoney,
      }
      co.getJyhsApi('money/Recorduser/balancePay', param, function(res) {
         wx.hideLoading()
         co.toastAllText(res.msg)
         if (res.code == 0) {
            console.log('res====', res)
            co.getJyhsApi("order/Order/updateOrderStatue", {
               Order_ID: _data.orderList.Order_ID,
               Order_Status: 4
            }, function(res) {
               if (res.code == 0) {
                  _this.getOrderDetail()
                  var msg = {
                     type: "payOrder",
                     money: _data.payMoney
                  }
                  if (_data.orderList.Biz_ID == 0) { //用户的
                     msg.to_user_id = _data.orderList.User_ID
                  } else { //企业的
                     msg.to_biz_id = _data.orderList.Biz_ID
                  }
                  ws.sendSocketMsg(JSON.stringify(msg), function(res) {
                     console.log("ws", res)
                  })
                  //回到订单页面
                  _this.back_order()
               }
            })
         }
      })
   },
   //回到订单页面
   back_order() {
      wx.reLaunch({
         url: '/02order/order',
      })
   },
   //微信支付
   wxPay() {
      var appid = app.Data.app.appid
      var param = {
         openid: app.Data.userInfo.Wx_Openid[appid],
         appid: appid,
         // key: 'bShYjNaUyIzGnEhSiAzIlIsNaHuOhZuF',
         // notify_url: 'http://api.igoyp.com/public/weixinpay/notify.php',
         // mch_id: '1514732771',
         money: _data.payMoney,
         abstract: '付款'
      }
      co.getShareApi('api/Wxpay/createParam', param, function(res) {
         if (res.code == 0) {
            _this.wxRequestPayment(res.data)
         } else {
            co.modal(res.msg)
         }
      })
   },
   wxRequestPayment(data) {
      data.success = function(result) {
         co.toastAllText('支付成功')
         console.log('【调起支付成功res】', result)
         _this.add_user_pay_record()
         _this.update_order_status(_data.orderList.Order_ID)
         _this.sendMiniProject()
         _this.back_order()
      }
      data.fail = function(result) {
         console.log('【调起支付失败res】', result)
      }
      //调起微信支付
      wx.requestPayment(data)
   },
   //更新订单状态
   update_order_status(orderid) {
      co.getJyhsApi("order/Order/updateOrderStatue", {
         Order_ID: orderid,
         Order_Status: 4
      }, function(res) {
         if (res.code == 0) {
            console.log(res.msg)
         }
      })
   },
   //添加支付记录
   add_user_pay_record() {
      var param = {
         Abstract: "微信付款",
         Pay_From: "wx_pay",
         Pay_To: "91_pay",
      }
      var param1 = {
         Abstract: "微信付款",
         Pay_From: "wx_pay",
         Pay_To: "91_pay",
      }
      if (_data.orderList.Biz_ID == 0) { //用户下的单
         //1.回收端人员添加记录
         param.User_ID = app.Data.userInfo.User_ID
         param.Payer = 'User_' + _data.orderList.User_ID //对方的id
         param.Money = -_data.payMoney
         co.getJyhsApi("money/Recorduser/userAdd", param, function(res) {
            if (res.code == 0) {
               console.log(res.msg)
               //2.用户收钱添加记录
               param1.Money = (_data.payMoney - _data.Extract)
               param1.User_ID = _data.orderList.User_ID
               param1.Payer = 'User_' + app.Data.userInfo.User_ID
               co.getJyhsApi("money/Recorduser/userAdd", param1, function(res) {
                  if (res.code == 0) {
                     console.log(res.msg)
                  } else {
                     console.log(res.msg)
                  }
               })
            } else {
               console.log(res.msg)
            }
         })
      } else { //企业下的单
         //1.回收端人员添加记录
         param.User_ID = app.Data.userInfo.User_ID
         param.Payer = 'Biz_' + _data.orderList.Biz_ID
         param.Money = -_data.payMoney
         co.getJyhsApi("money/Recorduser/userAdd", param, function(res) {
            if (res.code == 0) {
               console.log(res.msg)
               //2.企业收钱添加记录
               param1.Money = (_data.payMoney - _data.Extract)
               param1.Biz_ID = _data.orderList.Biz_ID
               param1.Payer = 'User_' + app.Data.userInfo.User_ID
               co.getJyhsApi("money/Recordbiz/bizAdd", param1, function(res) {
                  if (res.code == 0) {
                     console.log(res.msg)
                  } else {
                     console.log(res.msg)
                  }
               })
            } else {
               console.log(res.msg)
            }
         })
      }
   },
   //公众号发送模板消息
   sendTemplateMsg() {
      co.getShareApi("api/Weixin/sendTemplateMsg", {
         appid: "wx98efbee64850b8ff",
         secret: "7e6579a82566858427a852525875c205",
         openid: app.Data.userInfo.Wx_Openid['wx98efbee64850b8ff'],
         template_id: "A4fN0dwpd2TbYXtfQS_K_iw0ms4ZmkXzeHfLtWTjBL4",
         key_title: "充值,#22cc22",
         keyword1: "10,#22cc22",
         keyword2: "2018-10-12,#22cc22",
         key_remark: "充值成功,#22cc22",
         toUrl: "02order/orderDetail/orderDetail",
      }, function(res) {
         if (res.code == 0) {
            console.log(res.data)
         }
      })
   },
   //小程序发送模板消息
   sendMiniProject(formId) {
      co.getShareApi("api/Weixin/sendWxAppTemplateMsg", {
         appid: "wx3e9bb4b57e8b2eca",
         secret: "496f80d15d229107044228e5d9ca20fc",
         openid: app.Data.userInfo.Wx_Openid.wx3e9bb4b57e8b2eca,
         template_id: "16iYnlro6ZcXX5eCJn19MxfmQh11txJL3Sr5E",
         page: '04jyhs/accountAdmin/accountAdmin',
         formId: formId,
         keyword1: "2527537",
         keyword2: 0.01 + "元",
         keyword3: co.getFormatDate(2),
         keyword4: "充值未到账可联系18006938850"
      }, function(res) {
         if (res.code == 0) {
            console.log(res.data)
         }
      })
   },
   //联系企业或用户电话
   callPhone: function() {
      var phone = _data.orderList.Biz_Info.Biz_Phone ? _data.orderList.Biz_Info.Biz_Phone : _data.orderList.User_Info.User_Mobile
      wx.makePhoneCall({
         phoneNumber: phone,
      })
   },
})